package com.tomtop.core.logging;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.tomtop.core.utils.JSONUtils;

/**
 * Aop 访问日志
 * 
 * @author lenovo
 *
 */
@Aspect
@Order(1)
@Component
public class ServiceRunTimeAop {

	@Pointcut("within(@org.springframework.stereotype.Service *)")
	public void serviceRunTime() {
	}

	@Pointcut("within(@org.springframework.stereotype.Repository *)")
	public void repositoryRunTime() {
	}

	@Around("repositoryRunTime() || serviceRunTime()")
	public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {

		String methodName = point.getSignature().getName();
		String className = point.getTarget().getClass().getName();
		RunTimeLog.log(" 方法执行开始 {}.{},[参数]:{}", className, methodName);
		try {
			return point.proceed();
		} finally {
			Object[] param = point.getArgs();
			String params = JSONUtils.toJSONString(param);
			RunTimeLog.log(" 方法执行结束 {}.{},[参数]:{}", className, methodName, params);
		}
	}

}
